#include <stdio.h>
#include <stdbool.h>

#define MaxSize 10
typedef int ElemType;
typedef struct{
	ElemType data[MaxSize];
	int length;
}SqList;

bool ListInsert(SqList *L, int i, ElemType e){
	if(L->length == MaxSize){
		return false;
	}
	if(i < 1 || i > L->length + 1){ 
		return false;
	}
	for(int j = L->length-1; j >= i - 1; j--){
		L->data[j + 1] = L->data[j]; 
	}
	L->data[i-1]=e;
	L->length++;
	return true;
}

bool ListDelete(SqList *L, int i, ElemType *e){
	if(L->length == 0 ){
		return false;
	}
	if(i < 1 || i > L->length){ 
		return false;
	}
	*e = L->data[i-1];
	for(int j = i-1; j < L->length - 1; j++){
		L->data[j] = L->data[j+1]; 
	}
	L->length--;
	return true;
}

int LocateElem(SqList L, ElemType e){
	for(int j = 0; j < L.length-1; j++){
		if(e == L.data[j]){
			return j + 1;	
		}	
	}
	return 0;
}

bool GetElem(SqList L, int i, ElemType *e){
	if(L.length == 0 || i < 1 || i > L.length){
		return false;
	}
	*e = L.data[i-1];
	return true;
}

bool ClearList(SqList *L){
	L->length = 0;
	return true;
}

bool ListEmpty(SqList L){
	if(L.length == 0){
		return true;
	}else{
		return false;
	}
}

void ShowSqList(SqList L){
	for(int i = 0; i < L.length; i++){
		printf("%d ", L.data[i]);
	}
	printf("\nlength: %d\n", L.length);
}

int main(){
	SqList l = {
		{1, 2, 3, 4, 5},
		5,
	};
	ShowSqList(l);
	bool r = ListInsert(&l, 6, 6);
	if(!r){
		return 1;
	}	

	ShowSqList(l);
	r = ListInsert(&l, 2, 6);
	ShowSqList(l);

	ElemType e;
	r = ListDelete(&l, 6, &e);
	printf("%d\n", e);
	ShowSqList(l);

	ElemType f;
	r = ListDelete(&l, 3, &f);
	printf("%d\n", f);
	ShowSqList(l);
	return 0;
}


